#include "dialoglist.h"
#include "ui_dialoglist.h"
#include <QToolButton>
#include <QVector>
#include "widget.h"
#include <QMessageBox>

DialogList::DialogList(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DialogList)
{
    ui->setupUi(this);

    //设置标题
    setWindowTitle("myQQ 2021");

    //设置图标
    setWindowIcon(QPixmap(":/res/QQ.jpg"));

    //准备图标
    QList<QString> nameList;
    nameList << "典韦" << "后羿" << "老夫子" << "QQ管家"<<"亚瑟" << "甄姬" << "铠" << "马可";

    QStringList iconList;
    iconList << "dianwei" << "houyi" << "lafuzi" <<"QQ"<< "yase" << "zhenji" << "kai" << "make";

    QVector<QToolButton*> vToolBtn;    //创建容器保存记录
    static QVector<bool> isShow;

    //设置成员
    for(int i = 0; i < 8; ++i)
    {
        QToolButton* btn = new QToolButton;
        //设置名称
        btn->setText(nameList[i]);
        //设置头像
        QString str = QString(":/res/%1.jpg").arg(iconList.at(i));
        btn->setIcon(QPixmap(str));
        //设置头像大小
        btn->setIconSize(QPixmap(":/res/dianwei.jpg").size());
        //设置按钮风格  透明
        btn->setAutoRaise(true);
        //设置文字和图片一起显示
        btn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        //将成员加到垂直布局中
        ui->verticalLayout_2->addWidget(btn);
        vToolBtn.push_back(btn);
        //8个标识的默认构造
        isShow.push_back(false);
    }

    //对9个按钮 进行添加信号槽连接
    for(int i = 0; i < vToolBtn.size(); ++i){
        connect(vToolBtn[i],&QToolButton::clicked,[=]()mutable{
            //如果此窗口打开了，就不要再次打开
            if(isShow[i]){
                QString str = QString("%1窗口已经打开").arg(vToolBtn[i]->text());
                QMessageBox::warning(this,"警告",str);
                return;
            }

            isShow[i] = true;

            //弹出聊天对话框
            //构造时告诉窗口的名字  参数1 顶层方式弹出  参数2 窗口名字
            Widget* widget = new Widget(0,vToolBtn[i]->text());
            //设置窗口标题
            widget->setWindowTitle(vToolBtn[i]->text());
            widget->setWindowIcon(vToolBtn[i]->icon());
            widget->show();

            //关闭窗口将标识置为false
            connect(widget,&Widget::closeWidget,[=]()mutable{
                isShow[i] = false;
            });
        });
    }

}

DialogList::~DialogList()
{
    delete ui;
}




















